<template>
	<view class="swiper-wrap" :style="{height, padding}" v-if="goodsList.length||lists.length">
		<view class="swiper-con" >
			<template v-if="isSwiper==1">
				<swiper class="swiper" :autoplay="autoplay" :circular="circular" @change="swiperChange"
					:previous-margin="previousMargin" display-multiple-items="1">
					<swiper-item v-for="(item, index) in lists" :key="index">
						<view :data-item="item" style="width:100%;height:100%;" @click="goPage(item)">
							<u-image mode="aspectFill" :width="'calc(100% - ' + previousMargin + ')'" height="100%"
								:border-radius="radius" :src="item.image"></u-image>
						</view>
					</swiper-item>
				</swiper>
				<view class="dots" v-if="goodsList.length > 1">
					<view v-for="(item, index) in goodsList" :key="index"
						:class="'dot ' + (index == currentSwiper ? 'active' : '')"></view>
				</view>
			</template>
			
			<template v-else-if="isSwiper==2">
				<swiper class="new-swiper" :style="{height:nHeight}" :autoplay="autoplay" :circular="circular" @change="swiperChange"
					:previous-margin="previousMargin" display-multiple-items="1" :current="currentSwiper">
					<swiper-item v-for="(item, index) in goodsList" :key="index">
						<view :data-item="item" style="width:100%;height:100%;" @click="goPage(item)">
							<u-image mode="aspectFill" :width="'calc(100% - ' + previousMargin + ')'" height="100%"
								:border-radius="radius" :src="item.image"></u-image>
						</view>
						<view class="goods-dtl">
							<view class="goods-rmk black xs">
								{{item.params.remark}}
							</view>
							<price-format :color="colorConfig.primary" class="m-r-10" :first-size="34" :second-size="26"
								:subscript-size="26" :price="item.params.min_price" :weight="500"></price-format>
						</view>
					</swiper-item>
				</swiper>
				<view class="new-dots flex row-center" v-if="goodsList.length > 1">
					<view v-for="(item, index) in goodsList" :key="index"
						:class="'dot ' + (index == currentSwiper ? 'active' : '')"  @click="goThis(index)">
						<u-image mode="aspectFill" width="100%" height="100%" :src="item.params.image" border-radius="5"></u-image>
					</view>
				</view>
			</template>
			
			<template v-else-if="isSwiper==3">
				<swiper class="hot-swiper" :autoplay="autoplay" :circular="circular" @change="swiperChange"
					:previous-margin="previousMargin" display-multiple-items="1" :next-margin="nextMargin">
					<swiper-item v-for="(item, index) in goodsList" :key="index">
						<view :data-item="item" @click="goPage(item)"  :style="{
						transform: currentSwiper != index ? 'scaleY(0.9)' : 'scaleY(1)',
						margin: currentSwiper != index ? '0 20rpx' : 0,
					}">
							<u-image mode="aspectFill" width="100%" :height="nHeight"
								 :src="item.image"></u-image>
							<view class="hot-foot bg-white flex">
								<view class="goods-dtl flex-1">
									<view class="goods-rmk black xs">
										{{item.params.remark}}
									</view>
									<price-format :color="colorConfig.primary" class="m-r-10" :first-size="34" :second-size="26"
										:subscript-size="26" :price="item.params.min_price" :weight="500"></price-format>
									<button class="buy-btn">立即购买</button>
								</view>
								<view class="flex-1 flex row-center col-center">
									<u-image  mode="aspectFill" width="150rpx" height="150rpx" :src="item.params.image"></u-image>
								</view>
							</view>
						</view>
					</swiper-item>
				</swiper>
				<view class="hot-dots flex row-between" v-if="goodsList.length > 1">
					<u-line-progress active-color="#00632B" :percent="(currentSwiper+1)/goodsList.length
					*100" height="10" :show-percent="false" style="width: 300rpx;"></u-line-progress>
				</view>
			</template>
			
			<template v-lese v-for="(item, index) in goodsList">
				<view :key="index" :data-item="item" style="width:100%;height:100%;" @click="goPage(item)"
					v-if="index < 1">
					<u-image mode="aspectFill" :width="'calc(100% - ' + previousMargin + ')'" height="100%"
						:border-radius="radius" :src="item.image"></u-image>
				</view>
			</template>
		</view>
	</view>
</template>

<script>
	import {
		getAdList
	} from '@/api/store';
	import {
		paramsToStr
	} from '@/utils/tools'

	export default {
		data() {
			return {
				lists: [],
				goodsList:[],
				currentSwiper: 0
			};
		},
		props: {
			pid: {
				type: Number
			},
			adCode: {
				type: String
			},
			circular: {
				type: Boolean,
				default: true
			},
			autoplay: {
				type: Boolean,
				default: true
			},
			height: {
				type: String
			},
			radius: {
				type: String,
				default: '0'
			},
			padding: {
				type: String,
				default: '0rpx'
			},
			previousMargin: {
				type: String,
				default: "0rpx"
			},
			nextMargin: {
				type: String,
				default: "0rpx"
			},
			isSwiper: {
				type: Number,
				default: 0
			},
			newHeight: {
				type: String,
			}
		},
		created() {
			console.log("this.adCode",this.adCode)
			this.getAdListFun();
		},
		watch: {
			'pid': function(value) {
				this.getAdListFun();
			}
		},
		computed: {
			nHeight() {
				if (this.isSwiper==2) {
					return (this.newHeight-140) +'rpx'
				}
				if (this.isSwiper==3) {
					return (this.newHeight-200) +'rpx'
				}
			}
		},
		methods: {
			async getAdListFun() {
				const {
					code,
					data
				} = await getAdList({
					ad_code: this.adCode,
					pid: this.pid,
					terminal: 1,
				})
				if (code == 1) {
					this.lists = data
					this.goodsList=data
				}
			},
			goThis(e){
				this.currentSwiper = e
			},
			swiperChange(e) {
				this.currentSwiper = e.detail.current
			},
			goPage(item) {
				let {
					link,
					link_type,
					params,
					is_tab
				} = item;
				switch (link_type) {
					case 1:
						""
					case 2:
						if (is_tab) {
							this.$Router.pushTab({
								path: link
							});
						} else {
							this.$Router.push({
								path: link,
								query: params
							})
						}
						break;
					case 3:
						this.$Router.push({
							path: '/pages/webview/webview',
							query: {
								url: link
							}
						})
						break;
				}
			}
		}
	};
</script>
<style lang="scss">
	.swiper-wrap {
		overflow: hidden;
		box-sizing: content-box;
	
		.swiper-con {
			position: relative;
			height: 100%;
			overflow: hidden;
			transform: translateY(0);
		}
	
		.swiper {
			width: 100%;
			height: 100%;
			position: relative;
	
			.slide-image {
				height: 100%;
			}
	
		}
		
		.new-swiper {
			width: 100%;
			height: 100%;
			position: relative;
		
			.slide-image {
				height: 100%;
			}
			.goods-dtl {
				position: absolute;
				left: 20rpx;
				bottom: 20rpx;
				height: 80rpx;
				width: 300rpx;
				.goods-rmk{
					width: 800rpx;
					height: 40rpx;
					line-height: 40rpx;
				}
			}
		}
		
		.hot-swiper {
			width: 100%;
			height: 100%;

		
			.slide-image {
				height: 100%;
			}
			.hot-foot{
				height: 200rpx;
				
				.goods-dtl {
					width: 300rpx;
					margin: 0 60rpx;
				
					.goods-rmk{
						line-height: 42rpx;
						margin-bottom: 5rpx;
					
					}
					.buy-btn{
						border: 1px solid #00632B;
						font-size: 22rpx;
						padding: 0 10rpx;
						line-height: 30rpx;
						width: 140rpx;
						margin: 10rpx 0;
					}
				}
			}
		}
	
		.dots {
			position: absolute;
			left: 50%;
			transform: translateX(-50%);
			bottom: 20rpx;
			display: flex;
	
			.dot {
				width: 8rpx;
				height: 8rpx;
				border-radius: 50%;
				margin-right: 10rpx;
				background-color: #fff;
	
				&.active {
					width: 16rpx;
					border-radius: 8rpx;
					background-color: $-color-primary;
				}
			}
		}
		
		.new-dots {
			.dot {
				width: 100rpx;
				height: 100rpx;
				border-radius: 5rpx;
				margin: 20rpx 20rpx;
			
				&.active {
					border: 1px solid $-color-primary;
				}
			}
		}
		
		.hot-dots {
			position: absolute;
			left: 50%;
			transform: translateX(-50%);
			bottom: -10rpx;
		}
	}
</style>
